<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>《Java核心技术卷I》笔记——第四章 | CodeLife</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="本文为《Java核心技术卷I》的第四章的读书笔记，因为为Java的基础部分，因此大部分知识都已经掌握，主要对需要进一步理解和之前没有掌握的知识进行了摘录总结。">
<meta property="og:type" content="article">
<meta property="og:title" content="《Java核心技术卷I》笔记——第四章">
<meta property="og:url" content="http://jia199474.oschina.io/2016/02/22/Core-Java-Volumn-Chapter-4/index.html">
<meta property="og:site_name" content="CodeLife">
<meta property="og:description" content="本文为《Java核心技术卷I》的第四章的读书笔记，因为为Java的基础部分，因此大部分知识都已经掌握，主要对需要进一步理解和之前没有掌握的知识进行了摘录总结。">
<meta property="og:updated_time" content="2016-02-22T06:44:07.980Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="《Java核心技术卷I》笔记——第四章">
<meta name="twitter:description" content="本文为《Java核心技术卷I》的第四章的读书笔记，因为为Java的基础部分，因此大部分知识都已经掌握，主要对需要进一步理解和之前没有掌握的知识进行了摘录总结。">
  
  
    <link rel="icon" href="css/images/favicon.ico">
  
  
 <link href='//fonts.useso.com/css?family=Open+Sans:400italic,400,600' rel='stylesheet' type='text/css'>
 <link href="//fonts.useso.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">


  <link rel="stylesheet" href="/css/style.css" type="text/css">
  <link rel="stylesheet" href="/font-awesome/css/font-awesome.min.css" type="text/css">
  

  <!--

-->
  
</head>
<body>
  <div id="container">
    <header id="header">
  <div id="header-main" class="header-inner">
    <div class="outer">
      <a href="/" id="logo"><i class="logo" style="background-image: url(/css/images/logo.png)"></i><span class="site-title">CodeLife</span></a>
      <nav id="main-nav">
        
          <a class="main-nav-link" href="/.">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
          <a class="main-nav-link" href="/about">About</a>
        
      </nav>
      
        <nav id="sub-nav">
          <div class="profile" id="profile-nav">
            <a id="profile-anchor" href="javascript:;"><img class="avatar" src="/css/images/ava.png"><i class="fa fa-caret-down"></i></a>
          </div>
        </nav>
      
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" results="0" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit"> </button><input type="hidden" name="sitesearch" value="http://jia199474.oschina.io"></form>
      </div>
    </div>
  </div>
  <div id="main-nav-mobile" class="header-sub header-inner">
    <table class="menu outer">
      <tr>
        
          <td><a class="main-nav-link" href="/.">Home</a></td>
        
          <td><a class="main-nav-link" href="/archives">Archives</a></td>
        
          <td><a class="main-nav-link" href="/about">About</a></td>
        
        <td>
          <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" results="0" class="search-form-input" placeholder="Search"><input type="hidden" name="sitesearch" value="http://jia199474.oschina.io"></form>
        </td>
      </tr>
    </table>
  </div>
</header>

    <div class="outer">
      
        <aside id="profile">
  <div class="inner profile-inner">
    <div class="base-info profile-block">
      <img id="avatar" src="/css/images/ava.png">
      <h2 id="name">Zachary Jia</h2>
      <h3 id="title">A programmer</h3>
      <span id="location"><i class="fa fa-map-marker"></i>Beijing, China</span>
      <a id="follow" href="https://github.com/ZacharyJia">关注我</a>
    </div>
    <div class="article-info profile-block">
      <div class="article-info-block">
        17
        <span>文章</span>
      </div>
      <div class="article-info-block">
        13
        <span>标签</span>
      </div>
    </div>
    
    <div class="contact-info profile-block">
      <table class="contact-list">
        <tr>
          
          <td><a href="http://github.com/zacharyjia" target="_blank" title="github"><i class="fa fa-github"></i></a></td>
          
          <td><a href="/atom.xml" target="_blank" title="rss"><i class="fa fa-rss"></i></a></td>
          
        </tr>
      </table>
    </div>
    
    
  </div>
</aside>

      
      <section id="main"><article id="post-Core-Java-Volumn-Chapter-4" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-inner">
    
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      《Java核心技术卷I》笔记——第四章
    </h1>
  

        <div class="article-meta">
          
  <div class="article-date">
    <i class="fa fa-calendar"></i>
    <a href="/2016/02/22/Core-Java-Volumn-Chapter-4/">
      <time datetime="2016-02-22T06:10:33.000Z" itemprop="datePublished">2016-02-22</time>
    </a>
  </div>


          
        </div>
      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
                
        <p>本文为《Java核心技术卷I》的第四章的读书笔记，因为为Java的基础部分，因此大部分知识都已经掌握，主要对需要<strong>进一步理解</strong>和<strong>之前没有掌握</strong>的知识进行了摘录总结。</p>
<a id="more"></a>
<h2 id="一、_对面向对象思想的理解">一、 对面向对象思想的理解</h2><h3 id="对象的三个主要特性：">　对象的三个主要特性：</h3><ol>
<li>对象的行为（behaviour） —— 能够对对象施加哪些操作</li>
<li>对象的状态（state） —— 施加方法的时候，对象应该如何响应</li>
<li>对象的标识（identity） —— 如何辨别具有相同行为和状态的不同对象</li>
</ol>
<p>对象状态的改变必须通过调用方法实现， 如果不经过方法调用就可以改变对象状态，说明封装性遭到了破坏。</p>
<p>每个对象的标识永远是不同的， 状态常常存在差异。</p>
<p>对象的这些关键特征彼此相互影响。</p>
<h2 id="二、_日期的处理">二、 日期的处理</h2><p>不推荐使用Date类，而推荐使用GregorianCalendar类。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">new</span> GregorianCalendar(<span class="number">1991</span>, Calendar.DECEMBER, <span class="number">31</span>, <span class="number">23</span>, <span class="number">29</span>, <span class="number">29</span>);</span><br></pre></td></tr></table></figure>
<p>之前不知道有GregorianCalendar这个类，在处理日期的时候，用的一直是Date类，因此用了很多被标记为已经过期的API，如<code>getYear()</code>, <code>getMonth()</code>等。这次学习之后，了解到，<strong>当用到记录时间点的时候，使用Date类</strong>， 而<strong>处理日期的时候，使用GregorianCalendar类</strong>。</p>
<h2 id="三、getter的返回值">三、getter的返回值</h2><p>在返回可变对象的引用的时候，不能直接返回，因为其引用的对象是同一个，这样做会破坏其封装性。应该返回其克隆对象。</p>
<p>eg：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">return</span> date.clone();</span><br></pre></td></tr></table></figure>
<h2 id="四、Java的函数传递使用值传递">四、Java的函数传递使用值传递</h2><p><strong>Java中只有值传递</strong></p>
<p>方法参数的使用情况：</p>
<ul>
<li>一个方法不能修改一个基本数据类型的参数（数值型和布尔型）</li>
<li>一个方法可以改变一个对象参数的状态</li>
<li>一个方法不能让对象参数引用给一个新的对象</li>
</ul>
<h2 id="五、类的初始化步骤">五、类的初始化步骤</h2><ol>
<li>所有数据域被初始化为默认值（0， false，null）</li>
<li>按照在类生命中出现的次序，依次执行所有域初始化语句和初始化块</li>
<li>如果构造器第一行调用了第二个构造器，则执行第二个构造器主体</li>
<li>执行这个构造器的主体</li>
</ol>
<p>也就是说，如果一个数据域（成员变量）的初始化语句（直接写在变量定义之后的）调用了一个类的方法的话，这个方法可以先于构造器执行。</p>
<p>一个简单的例子如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Foo</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    &#123;</span><br><span class="line">        System.out.println(<span class="string">"This is a initial block of Foo!"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">int</span> a = assign();</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Foo</span><span class="params">()</span></span><br><span class="line">    </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"This is the construction method of Foo!"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">int</span> <span class="title">assign</span><span class="params">()</span></span><br><span class="line">    </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"this is the assign method of Foo!"</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">5</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这个类在创建新的实例的过程中，打印出的语句如下：</p>
<figure class="highlight nimrod"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">This</span> <span class="keyword">is</span> a initial <span class="keyword">block</span> <span class="keyword">of</span> <span class="type">Foo</span>!</span><br><span class="line">this <span class="keyword">is</span> the assign <span class="keyword">method</span> <span class="keyword">of</span> <span class="type">Foo</span>!</span><br><span class="line"><span class="type">This</span> <span class="keyword">is</span> the construction <span class="keyword">method</span> <span class="keyword">of</span> <span class="type">Foo</span>!</span><br></pre></td></tr></table></figure>
<p>其中，第一句和第二句的顺序，与初始化语句块和成员变量a的定义顺序相对应。</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://jia199474.oschina.io/2016/02/22/Core-Java-Volumn-Chapter-4/" data-id="cip19u34p000r1sqor8z1b2qz" class="article-share-link">分享到</a>
      
        <a href="http://jia199474.oschina.io/2016/02/22/Core-Java-Volumn-Chapter-4/#ds-thread" class="article-comment-link">评论</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Java/">Java</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/笔记/">笔记</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2016/02/23/PAT-1039/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">上一篇</strong>
      <div class="article-nav-title">
        
          PAT 1039 Course List for Student(25)
        
      </div>
    </a>
  
  
    <a href="/2016/01/14/2015-conclusion/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">下一篇</strong>
      <div class="article-nav-title">2015の总结&amp;&amp;2016の计划</div>
    </a>
  
</nav>


  
</article>

  
  <section id="comments">
    <!-- 多说评论框 start -->
    <div class="ds-thread" data-thread-key="post-Core-Java-Volumn-Chapter-4" data-title="《Java核心技术卷I》笔记——第四章" data-url="http://jia199474.oschina.io/2016/02/22/Core-Java-Volumn-Chapter-4/"></div>
    <!-- 多说评论框 end -->
    <!-- 多说公共JS代码 start (一个网页只需插入一次) -->
    <script type="text/javascript">
    var duoshuoQuery = {short_name:'zacharyjia'};
      (function() {
        var ds = document.createElement('script');
        ds.type = 'text/javascript';ds.async = true;
        ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
        ds.charset = 'UTF-8';
        (document.getElementsByTagName('head')[0] 
         || document.getElementsByTagName('body')[0]).appendChild(ds);
      })();
      </script>
    <!-- 多说公共JS代码 end -->
  </section>
  

</section>
      
        <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul id="recent-post" class="">
        
          <li>
            
            <div class="item-thumbnail">
              <a href="/2016/05/17/mysql-new-user-cannot-login/" class="thumbnail">
  
    <span style="background-image:url(http://7xn2d3.com1.z0.glb.clouddn.com/mysql01.png
)" alt="解决MySQL新建用户后无法登录问题" class="thumbnail-image"></span>
  
</a>
            </div>
            
            <div class="item-inner">
              <p class="item-category"></p>
              <p class="item-title"><a href="/2016/05/17/mysql-new-user-cannot-login/" class="title">解决MySQL新建用户后无法登录问题</a></p>
              <p class="item-date"><time datetime="2016-05-17T11:40:15.000Z" itemprop="datePublished">2016-05-17</time></p>
            </div>
          </li>
        
          <li>
            
            <div class="item-thumbnail">
              <a href="/2016/04/10/peterson-solution-turn-variable/" class="thumbnail">
  
    <span style="background-image:url(http://7xn2d3.com1.z0.glb.clouddn.com/no_turn.png
)" alt="Peterson算法中turn(will_wait)变量的作用" class="thumbnail-image"></span>
  
</a>
            </div>
            
            <div class="item-inner">
              <p class="item-category"></p>
              <p class="item-title"><a href="/2016/04/10/peterson-solution-turn-variable/" class="title">Peterson算法中turn(will_wait)变量的作用</a></p>
              <p class="item-date"><time datetime="2016-04-09T16:00:00.000Z" itemprop="datePublished">2016-04-10</time></p>
            </div>
          </li>
        
          <li>
            
            <div class="item-thumbnail">
              <a href="/2016/03/01/PAT-1052/" class="thumbnail">
  
    <span class="thumbnail-image thumbnail-none"></span>
  
</a>
            </div>
            
            <div class="item-inner">
              <p class="item-category"></p>
              <p class="item-title"><a href="/2016/03/01/PAT-1052/" class="title">PAT 1052 Linked List Sorting (25)</a></p>
              <p class="item-date"><time datetime="2016-03-01T15:50:02.000Z" itemprop="datePublished">2016-03-01</time></p>
            </div>
          </li>
        
          <li>
            
            <div class="item-thumbnail">
              <a href="/2016/02/29/PAT-1051/" class="thumbnail">
  
    <span class="thumbnail-image thumbnail-none"></span>
  
</a>
            </div>
            
            <div class="item-inner">
              <p class="item-category"></p>
              <p class="item-title"><a href="/2016/02/29/PAT-1051/" class="title">PAT 1051 Pop Sequence (25)</a></p>
              <p class="item-date"><time datetime="2016-02-29T12:55:35.000Z" itemprop="datePublished">2016-02-29</time></p>
            </div>
          </li>
        
          <li>
            
            <div class="item-thumbnail">
              <a href="/2016/02/29/PAT-1048/" class="thumbnail">
  
    <span class="thumbnail-image thumbnail-none"></span>
  
</a>
            </div>
            
            <div class="item-inner">
              <p class="item-category"></p>
              <p class="item-title"><a href="/2016/02/29/PAT-1048/" class="title">PAT 1048 Find Coins (25)</a></p>
              <p class="item-date"><time datetime="2016-02-29T12:20:58.000Z" itemprop="datePublished">2016-02-29</time></p>
            </div>
          </li>
        
      </ul>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">分类</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/life/">life</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/安全/">安全</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/编程/">编程</a><span class="category-list-count">2</span></li></ul>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">标签</h3>
    <div class="widget">
      <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Java/">Java</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/MySQL/">MySQL</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/NSCTF/">NSCTF</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/PAT/">PAT</a><span class="tag-list-count">6</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/git/">git</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/life/">life</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/多线程/">多线程</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/安全/">安全</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/总结/">总结</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/机器学习/">机器学习</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/汇编/">汇编</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/笔记/">笔记</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/算法/">算法</a><span class="tag-list-count">7</span></li></ul>
    </div>
  </div>

  
    
  <div class="widget-wrap widget-list">
    <h3 class="widget-title">链接</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="http://blog.zacharyjia.me">乱七八糟的想法</a>
          </li>
        
          <li>
            <a href="http://www.icodeyou.com/">牛逼的学长</a>
          </li>
        
      </ul>
    </div>
  </div>


  
  <div id="toTop" class="fa fa-chevron-up"></div>
</aside>
      
    </div>
    <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2015 - 2016 Zachary Jia<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>. Theme by <a href="http://github.com/ppoffice">PPOffice</a>
    </div>
  </div>
</footer>
    

<script type="text/javascript">
  var duoshuoQuery = {short_name:"zacharyjia"};
  (function() {
    var ds = document.createElement('script');
    ds.type = 'text/javascript';ds.async = true;
    ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
    ds.charset = 'UTF-8';
    (document.getElementsByTagName('head')[0] 
     || document.getElementsByTagName('body')[0]).appendChild(ds);
  })();
</script>



 <script src="http://libs.baidu.com/jquery/2.1.3/jquery.min.js"></script>




  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css" type="text/css">
  <script src="/fancybox/jquery.fancybox.pack.js" type="text/javascript"></script>


<script src="/js/script.js" type="text/javascript"></script>

  </div>
</body>
</html>